package com.suep.system.controller;


import com.suep.system.config.BaseR;
import com.suep.system.service.IQQEmailService;
import com.suep.system.utils.VerificationCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private IQQEmailService emailService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发送邮件的接口
     * @param to 收件人邮箱
     * @return 结果反馈
     */
    @PostMapping("/sendEmail/{to}")
    public BaseR sendMail(@PathVariable String to) {
        String verificationCode = VerificationCodeUtil.generateVerificationCode();
        try {
            String subject="WM系统验证码";
            String content="验证码为"+verificationCode+"，5分钟内有效";
            // 将验证码存储到 Redis 中，有效期为 5 分钟，用邮箱地址作为标识符，过期时间为5分钟
            redisTemplate.opsForValue().set(to, verificationCode, 5, TimeUnit.MINUTES);
            emailService.sendSimpleMail(to, subject, content);
            return new BaseR<>().ok("发送成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new BaseR<>().error("发送失败");
        }
    }
}
